/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db2docbook;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import muddb.DatabaseManager;

/**
 *
 * @author alhshk
 */
public class Driver {

  private final DatabaseManager dbm = DatabaseManager.getInstance();
  private final Connection con;
  private final DatabaseMetaData dbMeta;
  private Database db;

  public Driver() throws SQLException {
    con = dbm.getConnection();
    dbMeta = con.getMetaData();

    db = new Database();

//    dump("TypeInfo", dbMeta.getTypeInfo());
    final ResultSet rs = dbMeta.getTypeInfo();

    while (rs.next()) {
      final String typeName = rs.getString("TYPE_NAME");
      final int dateType = rs.getInt("DATA_TYPE");
      final boolean autoInc = rs.getBoolean("AUTO_INCREMENT");
      final String createParms = rs.getString("CREATE_PARAMS");

      db.add(new Type(typeName, dateType, autoInc, createParms));
    }

  }

  public void close() {
    if (con != null) {
      try {
        con.close();
      } catch (SQLException ex) {
        Logger.getLogger(Driver.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  }

  private void dump(String title, final ResultSet rs) throws SQLException {
    final ResultSetMetaData rsmd = rs.getMetaData();

    System.out.println("===== " + title + " =====");
    while (rs.next()) {
      for (int idx = 0; idx < rsmd.getColumnCount(); idx++) {
        final int col = idx + 1;
        System.out.println(rsmd.getColumnName(col) + ": " + rs.getString(col));
      }
      System.out.println();
    }
    System.out.println("======================");
  }

  private void process() throws SQLException {
    final ResultSet tables = dbMeta.getTables(null, null, "%", new String[]{"TABLE"});
    final ResultSetMetaData rsmd = tables.getMetaData();
//    dump(tables);

    while (tables.next()) {
      final String tName = tables.getString("TABLE_NAME");
      final String tCat = tables.getString("TABLE_CAT");
      final String tSchem = tables.getString("TABLE_SCHEM");

      Table table = new Table(tCat, tSchem, tName);
      db.add( table );
//      dump("Table " + tName, dbMeta.getColumns(tCat, tSchem, tName, "%"));
      ResultSet rs = dbMeta.getColumns(tCat, tSchem, tName, "%");
      while (rs.next()) {
        final String colName = rs.getString("COLUMN_NAME");
        final int dataType = rs.getInt("DATA_TYPE");
        final String typeName = rs.getString("TYPE_NAME");
        final int colSize = rs.getInt("COLUMN_SIZE");
        final int nullable = rs.getInt("NULLABLE");

        table.addColumn(colName, dataType, typeName, colSize, nullable == dbMeta.columnNullable);
      }
//      dump("pk: " + tName, dbMeta.getPrimaryKeys(tCat, tSchem, tName));
      rs = dbMeta.getPrimaryKeys(tCat, tSchem, tName);
      while (rs.next()) {
        final String colName = rs.getString("COLUMN_NAME");

        table.addPrimaryKey(colName);
      }
      dump("imported: " + tName, dbMeta.getImportedKeys(tCat, tSchem, tName));
      dump("exported: " + tName, dbMeta.getExportedKeys(tCat, tSchem, tName));
      System.out.println(table.toString());
    }
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) throws SQLException {

    final Driver m = new Driver();

    try {
      m.process();
    } finally {
      m.close();
    }
  }
}
